<?php
/**
 * 
 * User/IndexAction.class.php (设计师模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class GuestbookAction extends BaseAction
{

	function _initialize()
    {	
		parent::_initialize();
        $_GET =get_safe_replace($_GET);
        $this->url='index.php?g=User&m=Guestbook&a=index';
		/*if(!$this->_userid){
			$this->assign('jumpUrl',U('User/Login/index'));
			$this->error(L('nologin'));
			exit;
		}*/
        $id=intval($_GET['id']);
        if($id){
        $this->dao = M('Guestbook');
        $list=$this->dao->field('userid')->where("id=".$id)->find();
        if($this->_userid!=$list['userid']){
            $this->error(L('出错啦'));
            exit;
        }}

        $this->dao = M('User');
        $this->assign('bcid',0);
        $this->assign('title','餐饮空间_个人中心');
        $user = $this->dao->find($this->_userid);
        $this->user=$user;
        $this->assign('vo',$user);
		unset($_POST['status']);
		unset($_POST['groupid']);
		unset($_POST['amount']);
		unset($_POST['point']);

        
    }

    public function index()
    {


          $this->display();
    }
    public function fblog(){


        $catid='5';
        $this->Urlrule =F('Urlrule');


        $p= max(intval($_REQUEST[C('VAR_PAGE')]),1);
        if($catid){
            $cat = $this->categorys[$catid];
            $bcid = explode(",",$cat['arrparentid']);
            $jgbcid = $bcid;
            $bcid = $bcid[1];
            if($bcid == '') $bcid=intval($catid);
            if(empty($module))$module=$cat['module'];
            $this->assign('module_name',$module);
            unset($cat['id']);
            $this->assign($cat);
            $cat['id']=$catid;
            $this->assign('jgbcid',$jgbcid);
            $this->assign('catid',$catid);
            $this->assign('bcid',$bcid);
        }
        if($cat['readgroup'] && $this->_groupid!=1 && !in_array($this->_groupid,explode(',',$cat['readgroup']))){$this->assign('jumpUrl',URL('User-Login/index'));$this->error (L('NO_READ'));}
        $fields = F($this->mod[$module].'_Field');
        foreach($fields as $key=>$r){
            $fields[$key]['setup'] =string2array($fields[$key]['setup']);
        }
        $this->assign ( 'fields', $fields);


        $seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
        $this->assign ('seo_title',$seo_title);
        $this->assign ('seo_keywords',$cat['keywords']);
        $this->assign ('seo_description',$cat['description']);


        if($catid){
            $seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
            $this->assign ('seo_title',$seo_title);
            $this->assign ('seo_keywords',$cat['keywords']);
            $this->assign ('seo_description',$cat['description']);


            $where = 'userid='.$this->_userid;
            $typeid=$_GET['typeid'];
            if($typeid){
            $where .= ' and typeid='.$typeid;
            }
            // echo $where;exit;
            if(empty($cat['listtype'])){
                $this->dao= M($module);
                $count = $this->dao->where($where)->count();
                //echo $this->dao->getLastSql();exit;

                if($count){
                    import ( "@.ORG.Page" );
                    $listRows =  !empty($cat['pagesize']) ? $cat['pagesize'] : C('PAGE_LISTROWS');

                    $page = new Page ( $count, $listRows );

                    $page->urlrule = array (
                        "0"  => "/index.php?g=User&m=Guestbook&a=fblog",
                        "1" => "/index.php?g=User&m=Guestbook&a=fblog&p={\$page}"

                    );
                   // print_r($page->urlrule );exit;
                    $pages = $page->preshow();
                    $field = '*';
                    $order='listorder ,createtime desc,id desc';
                    if($_GET['order']){
                        $order=$_GET['order'].' desc';
                    }

                    $list = $this->dao->field($field)->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
                    //echo $this->dao->getLastSql();exit;
                    $this->assign('pages',$pages);
                    $this->assign('list',$list);
                    $this->assign('typeid',$typeid);
                }
                $template_r = 'list';
            }else{
                $template_r = 'index';
            }
        }else{
            $template_r = 'list';
        }
        $template = $cat['template_list'] ? $cat['template_list'] : $template_r;

//print_r($list);exit;



        $this->display();

    }


 //预约
    public function yuyue(){
        $designid=$_GET['userid'];
        $u = M('User');
        $list=$u->where("id=".$designid)->find();
        $data['designid']=$list['id'];
        $data['userid']=$this->_userid;
        $data['createtime']=time();
        $db=M('Yuyue');
        $result	=$db->add($data);
        if($result) {
            $u->where("id=".$designid)->setInc('yuyue_num');
            $this->success('预约成功！');
        }else{
            $this->error('预约失败，请稍后再试！');
        }

    }






    //添加修改页面
    public function edit(){


        $id=$_GET['id'];
        if($id){
        $this->dao = M('Case');
        $list=$this->dao->where("id=".$id)->find();
        $pics=$arr = explode(":",$list['pics']);
        //print_r($pics);exit;
        $this->assign('vo',$list);
        $this->assign('pics',$pics);
        }
        $this->display();
    }


public function insert()
{
    $sjsid=$_REQUEST["sjsid"];

    $mobileYzm = get_safe_replace($_POST['mobileyzm']);

//print_r($r);exit;
    $module=$_REQUEST["modulename"];
    $db=M($module);
    $_POST['ip'] = get_client_ip();
    if(empty($_POST['title'])){
        $this->error("姓名不能为空!");
    }
    if(empty($_POST['telephone'])){
        $this->error("手机不能为空!");
    }

    if(empty($_POST['bcity'])){
        $this->error("城市不能为空!");
    }

    if(empty($mobileYzm)){
        $this->error("验证码不能为空!");

    }

    if( md5($mobileYzm) != $_SESSION['tel_verify']){

        $this->error(L('验证码错误'));
    }



    $time_cha=time()-$_SESSION['time_verify'];
    if($time_cha > '120'){
        $this->error(L('验证码已过期'));
    }




    if(!$db->create($_POST)) {
        $this->error($db->getError());
    }

    $budget= $_POST['budget'];
    if($budget) {
        $budget_r = M('Label')->where("genre=19 and title='" . $budget . "'")->find();
        $db->budget_order = $budget_r['listorder'];
    }
    if($sjsid){
        $u = M('User');
        $list=$u->where("id=".$sjsid)->find();
        $db->sjsname=$list['username'];
        $db->sjsid=$list['id'];
        $db->typeid=1;
        $u->where("id=".$sjsid)->setInc('yuyue_num');
    }
    $db->createtime = time();
    $db->lang = LANG_ID;
    $db->keywords=$_POST['title'];
    // $message = str_replace(array('{verify}', '{sitename}'), array($verify, $this->Config['site_name']), $this->member_config['member_emailchecktpl']);


    $result	=$db->add();


    if($result) {
        //添加用户动态
        $userdate['title']='发布了【设计需求】';
        $userdate['url']='index.php?m=Guestbook&a=show&id='.$result;
        $userdate['userid']=$this->_userid;
        $userdate['conid']=$result;
		$userdate['module']='Guestbook';
        $userdate['createtime']=time();
        $userdate['lang']='1';
        $userdate['status']='1';
        $ud=M("Dynamic")->add($userdate);

        $message="有用户提交了设计需求<br> 称呼：".$_POST['title']."<br>电话：".$_POST['telephone']."<br>".jgarea($_POST['bprovince']).jgarea($_POST['bcity'])."<br>类型：".$_POST['itemType']."<br>面积：".$_POST['itemArea']."<br>预算：".$_POST['budget'];
        $r = sendmail($this->site_email,  '提交需求提示-' . $this->Config['site_name'], $message, $this->Config);

        $this->success('提交成功,等待审核！',U('Home/Index/index'));

    }else{
        $this->error('提交失败，请稍后再试！');
    }
}

    public function adds()
    {
        $massges=$_REQUEST["Guestbook"];
        $db=M('Guestbook');
        $verifyCode =$_POST['verifyCode'];
        if(md5($verifyCode) != $_SESSION['verify']){
            $this->error(L('error_verify'));
        }

        if(!$db->create($_POST)) {
            $this->error($db->getError());
        }
        $db->createtime = time();
        $db->lang = 1;
        $result =$db->add();


        if($result) {
            $this->success("提交成功");
        }else{
            $this->error("提交失败");
        }

    }

    public function delete()
    {
        $id = $_GET['id'];
        $userid = $this->_userid;
        $dao = M('Case');
        $list = $dao->field('pics,thumb')->where("id=" . $id)->find();
        $pics = explode(":", $list['pics']);

        $result = $dao->where("id=" . $id)->delete();
        if ($result) {
            @unlink('./' . $list['thumb']);
            foreach ($pics as $k => $v) {
                $r = @unlink($v);

            }

            $this->success('操作成功', $this->url);
        }


    }



}

